package cn.hesion.wordcount;
/**
 * ClassName: WordCountMapper <br/>
 * Description: <br/>
 * date: 2021/1/28 16:18<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @program: ClientDemo
 * @description: WordCountMapper
 * @author: hesion
 * @create: 2021-01-28 16:18
 **/
public class WordCountMapper extends Mapper<LongWritable , Text,Text, IntWritable> {

    Text k  = new Text();
    IntWritable v = new IntWritable(1);
    /**
        *@Author hst
        *@Description //TODO 这个函数每读一行的时候都会被调用
        *@Date 16:23 2021/1/28
        *@Param [key 偏移量，可以认为是行号, value 每一行的数据, context 上下文]
        * @return void
        **/
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 将值取出来
        String lineData = value.toString();
        // 空格分隔
        String[] words = lineData.split(" ");
        for (String word : words) {
           // 遍历每个单词，使其成为<word,1>
            k.set(word);
            context.write(k,v);
        }
    }
}
